package com.idTech.blocks;

import static net.minecraftforge.common.EnumPlantType.Cave;
import static net.minecraftforge.common.EnumPlantType.Crop;
import static net.minecraftforge.common.EnumPlantType.Desert;
import static net.minecraftforge.common.EnumPlantType.Nether;
import static net.minecraftforge.common.EnumPlantType.Plains;
import static net.minecraftforge.common.EnumPlantType.Water;

import java.util.Random;

import com.idTech.iDTechModBase;

import net.minecraft.block.Block;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.IPlantable;

public class TomatoCrop extends BlockFlower
{
	public TomatoCrop(int par1) {
		super(par1,Material.plants);
		
		this.setBlockBounds(0, 0, 0, 1, 1.5f, 1);
	    this.setCreativeTab(CreativeTabs.tabBlock);
	   
        
		// TODO Auto-generated constructor stub
	}
	public TomatoCrop(int par1, Material par2Material) {
		super(par1, par2Material);
		

        this.setCreativeTab(CreativeTabs.tabBlock);
		// TODO Auto-generated constructor stub
	}
	                     
	public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z)
	{
		return null;
	}
    
	public int getRenderType()
	{
		return 6;
	}
	public boolean isOpaqueCube()
	{
		return false;
	}
    public void registerIcons(IconRegister iconReg)
    {
    	blockIcon = iconReg.registerIcon("iDTechModBase:tomatoCrop");
    }
    
    public int getBlockTextureFromSideAndMetadata (int side, int metadata)
    {
    	return 32 + metadata;
    }
    
    public void updateTick( World world, int x, int y, int z, Random random)
    {
    	if (world.getBlockMetadata(x, y, z)== 1)
    	{
    		return;
    	}
    	if (random.nextInt(isFertile(world, x, y-1, z)? 12:25) != 0)
    	{
    		return;
    	}
    	world.setBlockMetadataWithNotify(x, y, z, 1,0);
    }
    
    public void onNeighborChange(World world, int x, int y, int z, int neighborId)
    {
    	if (!canBlockStay(world, x,y,z))
    	{
    		dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
    		world.setBlock(x, y, z, 0);
    		
    	}
    }
    public boolean canBlockStay(World world, int x, int y, int z)
    {
    	Block soil = blocksList[world.getBlockId(x, y-1, z)];
    	return (world.getFullBlockLightValue(x, y, z) >= 8 || world.canBlockSeeTheSky(x, y, z)) && (soil != null && soil.canSustainPlant(world, x, y-1, z, ForgeDirection.UP,iDTechModBase.tomatoSeeds)); 
    			
    
    }
    
    public int idDropped(int metadata, Random r, int p)
    {
    	switch (metadata)
    	{
    	case 0:
    		return iDTechModBase.tomatoSeeds.itemID;
    	case 1:
    		return iDTechModBase.tomatoFruit.itemID;
    	default :
    		return -1;
    		
    	}
    }
    
    public int idPicked(World world, int x, int y, int z)
    {
    	return iDTechModBase.tomatoSeeds.itemID;
    }
    @Override
    public EnumPlantType getPlantType(World world, int x, int y, int z)
    {
        if (blockID == crops.blockID        ) return Crop;
        if (blockID == deadBush.blockID     ) return Desert;
        if (blockID == waterlily.blockID    ) return Water;
        if (blockID == mushroomRed.blockID  ) return Cave;
        if (blockID == mushroomBrown.blockID) return Cave;
        if (blockID == netherStalk.blockID  ) return Nether;
        if (blockID == sapling.blockID      ) return Plains;
        if (blockID == melonStem.blockID    ) return Crop;
        if (blockID == pumpkinStem.blockID  ) return Crop;
        if (blockID == tallGrass.blockID    ) return Plains;
        return Plains;
    }

    @Override
    public int getPlantID(World world, int x, int y, int z)
    {
        return blockID;
    }

    @Override
    public int getPlantMetadata(World world, int x, int y, int z)
    {
        return world.getBlockMetadata(x, y, z);
    }

}
